package com.jt.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.mapper.ItemMapper;
import com.jt.mapper.ItemParamMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.pojo.ItemParam;
import com.jt.service.ItemService;
import com.jt.utils.Assert;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 冬虫虫
 */
@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private ItemMapper itemMapper;

	@Autowired
    private ItemDescMapper itemDescMapper;

	@Autowired
	private ItemParamMapper itemParamMapper;

	@Override
	public EasyUITable findItemByPage(Integer page, Integer rows) {
		//1.参数校验
		Assert.isValid(page<1,"请输入正确的页码值");
		Assert.isValid(rows<1,"每页至少有一条查询记录");
		//2.利用MP方式实现分页查询,需要配置分页拦截器
		IPage<Item> iPage = new Page<>(page, rows);
		QueryWrapper<Item> queryMapper = new QueryWrapper<>();
		queryMapper.orderByDesc("updated");
		IPage<Item> resultPage = itemMapper.selectPage(iPage, queryMapper);
		List<Item> items = resultPage.getRecords();
		int rowCount = (int)resultPage.getTotal();
		return new EasyUITable(rowCount,items);

	}

	@Transactional(rollbackFor = RuntimeException.class)
	@Override
	public void saveItem(Item item, ItemDesc itemDesc, ItemParam itemParam) {
		//参数校验
		Assert.isValid(item==null,"请先填写商品信息!");
		//设置商品信息
        item.setStatus(1).setCreated(new Date()).setUpdated(item.getCreated());
		int itemRows=itemMapper.insert(item);
		Assert.isServiceValid(itemRows==0,"新增商品失败!");
		//MP操作时,当item入库后会将主键自动回显
		itemDesc.setItemId(item.getId()).setCreated(item.getCreated()).setUpdated(item.getCreated());
		int itemDescRows = itemDescMapper.insert(itemDesc);
		Assert.isServiceValid(itemDescRows==0,"新增商品描述失败!");
		itemParam.setItemId(item.getId()).setCreated(item.getCreated()).setUpdated(item.getCreated());
		int itemParamRows = itemParamMapper.insert(itemParam);
		Assert.isServiceValid(itemParamRows==0,"新增商品规格参数失败!");
	}

	@Transactional(rollbackFor = RuntimeException.class)
	@Override
	public void updateItem(Item item,ItemDesc itemDesc,ItemParam itemParam) {
		//参数校验
		Assert.isValid(item==null,"请先填写商品信息!");
		//修改商品信息
        item.setUpdated(new Date());
		int rows = itemMapper.updateById(item);
		Assert.isServiceValid(rows==0,"修改商品信息失败!");
		//修改商品描述信息
		itemDesc.setItemId(item.getId()).setUpdated(new Date());
		int itemDescRows = itemDescMapper.updateById(itemDesc);
		Assert.isServiceValid(itemDescRows==0,"修改商品描述信息失败!");
		//修改商品参数信息
		itemParam.setItemId(item.getId()).setCreated(item.getUpdated()).setUpdated(item.getUpdated());
		QueryWrapper<ItemParam> paramQuery=new QueryWrapper<>();
		paramQuery.eq("item_id",itemParam.getItemId());
		int itemParamRows = itemParamMapper.update(itemParam,paramQuery);
		Assert.isServiceValid(itemParamRows==0,"修改商品规格参数失败!");
	}

    @Override
    public void updateItemStatus(Long[] ids, Integer status) {
        //参数校验
        Assert.isValid(ids.length==0,"未选中商品!");
        //修改商品信息
        Item item = new Item();
        item.setStatus(status).setUpdated(new Date());
        UpdateWrapper<Item> updateWrapper = new UpdateWrapper<>();
        List<Long> idList = Arrays.asList(ids);
        updateWrapper.in("id",idList);
        int rows = itemMapper.update(item, updateWrapper);
        //结果验证
        Assert.isServiceValid(rows==0,"修改商品状态失败!");
    }

    @Transactional(rollbackFor = RuntimeException.class)
	@Override
	public void deleteItems(Long[] ids) {
		//参数校验
		Assert.isValid(ids.length==0,"请先选中要删除的商品!");
		//删除商品(mapper方式,不要与MP方法重名)
        int rows=itemMapper.deleteItems(ids);
        //删除商品描述
		QueryWrapper queryWrapper = new QueryWrapper<>();
		List<Long> idList = Arrays.asList(ids);
		queryWrapper.in("item_id", idList);
		itemDescMapper.delete(queryWrapper);
		//结果验证
		Assert.isServiceValid(rows==0,"删除商品失败!");
	}

	@Override
	public ItemDesc findItemDescById(Long itemId) {
		QueryWrapper<ItemDesc> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("item_id",itemId);
		ItemDesc itemDesc = itemDescMapper.selectOne(queryWrapper);
		Assert.isServiceValid(itemDesc==null,"查询商品描述信息失败");
		return itemDesc;
	}

	@Override
	public Item findItemById(Long itemId) {
		Assert.isValid(itemId<=0,"您所查询商品可能已经不存在");
		Item item = itemMapper.selectById(itemId);
		Assert.isServiceValid(item==null,"您所查询商品可能已经不存在");
		return item;

	}

	@Override
	public ItemParam findItemParamById(Long itemId) {
		Assert.isValid(itemId<=0,"您所查询商品可能已经不存在");
		QueryWrapper<ItemParam> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("item_id",itemId);
		ItemParam itemParam = itemParamMapper.selectOne(queryWrapper);
		return itemParam;
	}
}
